package com.pesking.core.sqlparser.processor;

import java.util.Arrays;
import java.util.List;

import org.apache.commons.lang3.StringUtils;

import com.pesking.core.exception.SystemException;
import com.pesking.core.parameter.Parameter;
import com.pesking.core.sqlparser.parameter.SqlParameter;
import com.pesking.core.sqlparser.parameter.SqlParameterProcessor;
import com.pesking.core.util.LogUtils;

public class AttachmentDownloadUrlSqlParameterHandler extends SqlParameterProcessor {


	@Override
	public String getTitle() {
		return "附件下载参数";
	}

	@Override
	public String getDescription() {
		return this.getTitle() + " 生成附件下载地址,生成地址形如：/core/attachment/download/{id}";
	}

	@Override
	public void handle(StringBuilder sbSQL,
			Parameter parameter, List<SqlParameter> processors,SqlParameter sqlParameter) {
		String attachmentIdName = sqlParameter.getName();
		if (StringUtils.isBlank(attachmentIdName)) {
			throw new SystemException("使用方法：[DOWNLOAD:附件ID参数名]");
		}
		String logoId = parameter.getParameter(attachmentIdName);
		if (StringUtils.isBlank(logoId)) {
			LogUtils.debug("根据参数[" + attachmentIdName + "]从参数中取不到值");
			sbSQL.append("''");
		}
		else {
			sbSQL.append("'/core/attachment/download/").append(logoId).append("'");
		}
	}


    @Override
	public List<String> getSqlParameterType() {
		return Arrays.asList(new String []{"DOWNLOAD"});
	}
}
